package com.xs.shop.goods.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xs.shop.goods.model.vo.GoodsDetailsVO;
import com.xs.shop.goods.model.vo.GoodsListDetailsVO;
import com.xs.shop.goods.model.vo.GoodsPageVO;
import com.xs.shop.goods.model.entity.Goods;
import com.xs.shop.goods.service.mapper.GoodsMapper;
import com.xs.shop.goods.service.service.GoodsImageService;
import com.xs.shop.goods.service.service.GoodsService;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品信息表 服务实现类
 * </p>
 *
 * @author xs
 * @since 2025-07-10
 */
@Slf4j
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Resource
    private GoodsImageService goodsImageService;

    @Override
    public IPage<GoodsPageVO> pageQuery(Integer current, Integer pageSize, String name, Long categoryId, Byte isShow) {
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();

        // 商品名模糊查询
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.like(Goods::getName, name);
        }

        // 分类查询
        if (categoryId != null) {
            queryWrapper.eq(Goods::getCategoryId, categoryId);
        }

        // 状态查询
        if (isShow != null) {
            queryWrapper.eq(Goods::getStatus, isShow);
        }

        // 分页查询
        Page<Goods> page = this.page(new Page<>(current, pageSize), queryWrapper);
        IPage<GoodsPageVO> pageVO = new Page<>();
        BeanUtils.copyProperties(page, pageVO);

        List<Goods> records = page.getRecords();

        List<GoodsPageVO> goodsPageVOList = new ArrayList<>();
        for (Goods goods : records) {
            GoodsPageVO goodsPageVO = new GoodsPageVO();
            BeanUtils.copyProperties(goods, goodsPageVO);
            //todo 补充字段
            goodsPageVO.setSales(0);
            goodsPageVO.setTags(List.of("新品", "热卖"));
            //补充图片
            goodsPageVO.setImageUrl(goodsImageService.getMainByGoodsId(goods.getId()));
            goodsPageVOList.add(goodsPageVO);
        }

        pageVO.setRecords(goodsPageVOList);
        return pageVO;
    }

    @Override
    public GoodsDetailsVO findDetailsById(Long id) {
        Goods goods = this.getById(id);
        GoodsDetailsVO goodsDetailsVO = new GoodsDetailsVO();
        BeanUtils.copyProperties(goods, goodsDetailsVO);
        //todo 补充字段
        goodsDetailsVO.setSales(0);
        goodsDetailsVO.setTags(List.of("新品", "热卖"));
        goodsDetailsVO.setImageUrl(goodsImageService.getImagesByGoodsId(goods.getId()));
        return goodsDetailsVO;
    }

    @Override
    public List<GoodsListDetailsVO> findGoodsListDetailByIds(List<Long> ids) {
        List<Goods> goodsList = this.listByIds(ids);
        List<GoodsListDetailsVO> goodsListDetailsVOList = new ArrayList<>();
        for (Goods goods : goodsList) {
            GoodsListDetailsVO goodsListDetailsVO = new GoodsListDetailsVO();
            BeanUtils.copyProperties(goods, goodsListDetailsVO);
            goodsListDetailsVO.setImageUrl(goodsImageService.getMainByGoodsId(goods.getId()));
            goodsListDetailsVOList.add(goodsListDetailsVO);
        }
        return goodsListDetailsVOList;
    }
}
